package com.monster.controller;

import com.monster.dto.SetmealDTO;
import com.monster.entity.QueryPageBean;
import com.monster.entity.Result;
import com.monster.service.SetMealService;
import com.monster.utils.aliyunoss.AliyunUtils;
import com.monster.utils.redis.RedisUtil;
import com.monster.utils.resources.RedisConstant;
import com.monster.utils.resources.UploadUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
public class SetMealController {
    @Reference
    SetMealService setMealService;
    //图片上传到阿里云/web/setmeal/upLoadJpg

    @PostMapping("web/setmeal/upLoadJpg")
    public Result upLoadJpg(@RequestParam("imgFile")MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        String uuidFileName = UploadUtils.generateRandonFileName(originalFilename);
        try {
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //添加到set集合
        RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,uuidFileName);
        //添加到redis并设置时间
        RedisUtil.set(RedisConstant.SINGLE_PIC+uuidFileName,uuidFileName,5, TimeUnit.MINUTES);
        return new Result(uuidFileName);
    }
    @PostMapping("web/setmeal/addOrUpdata")
    public Result addOrUpdata(@RequestBody SetmealDTO setmealDTO){
        return new Result(setMealService.addOrUpdataCheckGroupDTO(setmealDTO));
    }
    @PostMapping("web/setmeal/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        return new Result(setMealService.findPage(queryPageBean));
    }
    //编辑的数据回显
    @GetMapping("web/setmeal/showDataBySid/{id}")
    public Result showDataByGid(@PathVariable("id") Integer id){
        return new Result(setMealService.showDataBySid(id));
    }
    //数据删除
    @DeleteMapping("web/setmeal/deleteById/{id}")
    public Result deleteById(@PathVariable("id") int id){
        return new Result(setMealService.deleteById(id));
    }
}
